package com.hl.card.modular.packBack.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import sun.misc.BASE64Decoder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.persistence.dao.GoodsMapper;
import com.hl.card.common.persistence.model.Goods;
import com.hl.card.common.util.DateUtils;
import com.hl.card.config.properties.OSSClientUtil;
import com.hl.card.modular.packBack.service.PoetryService;

@Service
public class PoetryServiceImpl implements PoetryService{
	@Autowired
	private RedisTemplate<String,String> redisTemplate;
	@Autowired
	private GoodsMapper goodsMapper;
	   @Autowired
	    private OSSClientUtil oSSClientUtil;
	
	/**
	 * 获取下拉的值
	 * @return
	 */
    public List<String> getTree()
    {
    	List<Goods> goodList=goodsMapper.selectGoodsByCodePrefix();
    	List<String> newGoodsList=new ArrayList<String>();
    	goodList.forEach((goods)->{
    	 	String[] childGoods= goods.getSeries().split(" ");
        	newGoodsList.addAll(Arrays.asList(childGoods));
    	
    	});
        return newGoodsList;
    }
    
    /**
     * 添加方法
     */
    public void poetryAdd(Map<String,String> map) throws Exception
    {
    	JSONObject object=new JSONObject();
    	if(map.get("photo")!=null&&map.get("photo").length()>0){
			byte[]  s=getInputStream(map.get("photo"));
			String name = oSSClientUtil.uploadImg2Oss(s,map.get("suffix"));
			String imgUrl = oSSClientUtil.getImgUrl(name);
			object.put("num", Integer.parseInt(map.get("num")));
			object.put("title", map.get("title"));
			object.put("photo", imgUrl);
		    String subjects=map.get("subjects");
		    if(map.get("poetryType").equals("ZHSC"))
		    {
		    	object.put("awardValue", map.get("awardValue"));
		    }
		    JSONObject subjectObject=JSONObject.parseObject(subjects);
		     StringBuffer sb=new StringBuffer();
		     for(String s_key:subjectObject.keySet())
		     {
		    	 JSONObject json=(JSONObject) subjectObject.get(s_key);
		    	 StringBuffer z_string=new StringBuffer();
		    	 if(map.get("poetryType").equals("ZHSC"))
		    	 {   
		    		 List<String> list=(List<String>) json.get("Z");
		    		
		    		 for(String key:list)
		    		 {
		    			 sb.append(key+"-"+json.getString(key)+"|");
		    			 z_string.append(key);
		    			 
		    		 }
		    		 json.put("Z", z_string);
		    		 subjectObject.put(s_key, json);

		    	 }else
		    	 {
		    		 List<String> list=(List<String>) json.get("Z");
		    		 for(String key:list)
		    		 {
		    			 z_string.append(key);
		    		 }
		    		 json.put("Z", z_string);
		    	 }
		    	 object.put(s_key, subjectObject.get(s_key));    //循环存储
		
		    	
		     }
		     if(sb.length()>0)
		     {
		    	 sb.deleteCharAt(sb.length() - 1);
		    	 object.put("poetry", sb);
		     }
			
		      
			  String code= map.get("poetryType");
		      Goods good = goodsMapper.selectByCode(code);
		      String nowtype=getCode(code);
		      good.setNowtype(nowtype);
		      good.setSeries(good.getSeries()+" "+nowtype);
		      good.setUpdatetime(DateUtils.getNow("yyyy-MM-dd HH:mm:ss"));
		      goodsMapper.updateById(good);
			  redisTemplate.opsForValue().set(nowtype,object.toJSONString());
		}
    }
    
    /**
     *  获取编号
     * @param fix
     * @return
     */
    public String getCode(String fix)
    {
    	return fix+DateUtils.getNow("yyyyMMddHHmmss");
    }
    /**
	 * 解析base64转换为byte[]
	 * @param data
	 * @return
	 * @throws IOException
	 */
   public byte[] getInputStream(String data) throws IOException
   {
	      // 通过base64来转化图片
	   data = data.replaceAll("^data:image/(png|gif|jpg|jpeg|bmp|tif|psd|ICO);base64,", "");      
       BASE64Decoder decoder = new BASE64Decoder();
       // Base64解码      
       byte[] imageByte = null;
       try {
           imageByte = decoder.decodeBuffer(data);      
           for (int i = 0; i < imageByte.length; ++i) {      
               if (imageByte[i] < 0) {// 调整异常数据      
                   imageByte[i] += 256;      
               }      
           }      
       } catch (Exception e) {
            e.printStackTrace(); 
       }   
	   return imageByte;
    }

}
